class EnemyController extends Combatants
   placeable;

//var() pawn model;

var Status poison;
var Status defBuff;
var Status atkBuff;
var Status agiBuff;
var Status magBuff;
var Status accBuff;
var Status mute;
var Status virus;
var Status stone;
var Status berserk;

var int i;

var BattleController b;
var BattleController z;

//var Equipment helmet;
//var Equipment armor;
//var Equipment weapon;
//var Equipment subWeapon;
//var Equipment boots;
//var Equipment gloves;
//var Equipment accessory;

var() Equipment gear[7];

var bool allDead;

enum equip
{
     EQ_helmet,
     EQ_armor,
     EQ_weapon,
     EQ_subWeapon,
     EQ_boots,
     EQ_gloves,
     EQ_accessory
};

simulated event PostBeginPlay ()
{
   hp = 5;
   maxHP = 5;
   mp = 0;
   maxMP = 0;
   strength = 8;
   dexterity = 10;
   vitality = 10;
   agility = 50;
   intelegence = 5;
   wisdom = 5;
   luck = 5;
   timer = 0;

   foreach AllActors(class 'BattleController', z)
   {
      b = z;
   }
}

function advanceTime()
{
   timer += agility;
}

function int getTime()
{
   return timer;
}

function equipNewGear(Equipment newGear)
{
   local int slot;
   slot = newGear.getType();
   if (gear[slot] != None)
   {
      maxHP -= gear[slot].getHP();
      maxMP -= gear[slot].getMP();
   }

   gear[slot] = newGear;
   maxHP += gear[slot].getHP();
   maxMP += gear[slot].getMP();
}

function int getHp()
{
    return hp;
}

function setHp(int newHp)
{
    hp = newHp;
}

function int getMp()
{
    return mp;
}

function setMp(int newMp)
{
    mp = newMp;
}

function pollStatus()
{
    if (stone.getdurration() >0)
    {
        GotoState ('NotTurn');
        //updateStatus (stone);
        return;
    }

    if (accBuff.getDurration() >0)
    {
        updateStatus (accBuff);
    }

    if (agiBuff.getDurration() >0)
    {
        updateStatus (agiBuff);
    }

    if (atkBuff.getDurration() >0)
    {
        updateStatus (atkBuff);
    }

    if (defBuff.getDurration() >0)
    {
        updateStatus (defBuff);
    }

    if (magBuff.getDurration() >0)
    {
        updateStatus (magBuff);
    }

    if (mute.getDurration() >0)
    {
       updateStatus (mute);
    }

    if (poison.getDurration() >0)
    {
        setHp (hp-poison.getEffect());
        updateStatus (poison);
    }

    if (berserk.getdurration() >0)
    {
        //attackEnemy (b.player[0]);
        updateStatus (berserk);
    }
}

function setStatus(int duration, int effect, int type)
{
    switch (type)
    {
        case ST_poison:
           if (poison.getDurration() <=0)
           {
              poison.setDurration(duration);
              poison.setEffect(effect);
           }
           break;
        case ST_def:
           if (defBuff.getDurration() <=0)
           {
              defBuff.setDurration(duration);
              defBuff.setEffect(effect);
           }
           break;
        case ST_atk:
           if (atkBuff.getDurration() <=0)
           {
              atkBuff.setDurration(duration);
              atkBuff.setEffect(effect);
           }
           break;
        case ST_agi:
           if (agiBuff.getDurration() <=0)
           {
              agiBuff.setDurration(duration);
              agiBuff.setEffect(effect);
           }
           break;
        case ST_mag:
           if (magBuff.getDurration() <=0)
           {
              magBuff.setDurration(duration);
              magBuff.setEffect(effect);
           }
           break;
        case ST_acc:
           if (accBuff.getDurration() <=0)
           {
              accBuff.setDurration(duration);
              accBuff.setEffect(effect);
           }
           break;
        case ST_mute:
           if (mute.getDurration() <=0)
           {
              mute.setDurration(duration);
              mute.setEffect(effect);
           }
           break;
        case ST_virus:
           if (virus.getDurration() <=0)
           {
              virus.setDurration(1);
              virus.setEffect(1);
           }
           break;
        case ST_stone:
           if (stone.getDurration() <=0)
           {
              stone.setDurration(1);
              stone.setEffect(1);
           }
           break;
        case ST_berserk:
           if (berserk.getDurration() <=0)
           {
              berserk.setDurration(duration);
              berserk.setEffect(1);
           }
           break;
        default:
           break;
    }
}

function updateStatus(Status currentStatus)
{
   currentStatus.decrementDurration();
}

function int getTotalVitality()
{
   local int totalVitality;
   totalVitality = vitality;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalVitality += gear[i].getVitality();
      }
   }
   return totalVitality;
}

function int getTotalAgility()
{
   local int totalAgility;
   totalAgility = agility;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalAgility += gear[i].getagility();
      }
   }
   return totalAgility;
}

function int getTotalStrength()
{
   local int totalStrength;
   totalStrength = strength;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalStrength += gear[i].getstrength();
      }
   }
   return totalStrength;
}

function int getTotalDexterity()
{
   local int totalDexterity;
   totalDexterity = dexterity;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalDexterity += gear[i].getdexterity();
      }
   }
   return totalDexterity;
}

function int getTotalLuck()
{
   local int totalLuck;
   totalLuck = luck;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalLuck += gear[i].getluck();
      }
   }
   return totalLuck;
}

function int getTotalIntelegence()
{
   local int totalIntelegence;
   totalIntelegence = intelegence;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalIntelegence += gear[i].getintelegence();
      }
   }
   return totalIntelegence;
}

function int getTotalWisdom()
{
   local int totalWisdom;
   totalWisdom = wisdom;
   for (i = EQ_helmet; i <= EQ_accessory; i++)
   {
      if (gear[i] != None)
      {
         totalWisdom += gear[i].getwisdom();
      }
   }
   return totalWisdom;
}

function attackEnemy(int target)
{
   local int damage;
   local int hitChance;
   local int critChance;
   local bool hit;

   //play attack animation
   hitChance = rand(100);

   hit = b.calculateHit(getTotalDexterity(), accBuff.getEffect(), hitChance, target);

   if (hit)
   {
      critChance = rand (20);
      if (critChance > luck)
      {
         damage = 2 * (getTotalStrength() + atkBuff.getEffect() + rand(getTotalStrength()/4));
         b.doDamage(target, damage, 0, 0, 0, 0);
      }
      else
      {
         damage = getTotalStrength() + atkBuff.getEffect() + rand(getTotalStrength()/4);
         b.doDamage(target, damage, 0,0,0,0);
      }
   }
   gotoState ('notTurn');
}

function getHurt (int damage, int fire, int water, int earth, int wind)
{
   local int defence;
   defence = (getTotalVitality() + defBuff.getEffect() / 2);
   setHP(getHP() - (damage - defence));
}

function startTurn()
{
}

auto state notTurn
{
   function startTurn ()
   {
      gotoState ('turn');
   }

Begin:
   //play idle animation
}

state turn
{

Begin:
   gotoState ('notTurn');
}

state dead
{
}

defaultproperties
{
  gear[EQ_helmet] = None;
  gear[EQ_armor] = None;
  gear[EQ_weapon] = None;
  gear[EQ_subWeapon] = None;
  gear[EQ_boots] = None;
  gear[EQ_gloves] = None;
  gear[EQ_accessory] = None;
}

